//
//  TPBaseViewController.m
//  TradingPlatform
//
//  Created by 程强 on 15/10/23.
//  Copyright © 2015年 程强. All rights reserved.
//

#import "TPBaseViewController.h"
#import "MBProgressHUD.h"

@interface TPBaseViewController ()
@property (strong, nonatomic) MBProgressHUD *hud;
@end

@implementation TPBaseViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setLeftBarItem];
}
- (void)setLeftBarItem
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(gotoBack)];
}
- (void)gotoBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)showHUD
{
    if (!_hud) {
        self.hud =[[MBProgressHUD alloc] initWithView:self.view];
        _hud.mode = MBProgressHUDModeIndeterminate;
        _hud.removeFromSuperViewOnHide = YES;
    }
    [self.view addSubview:_hud];
    [_hud showAnimated:YES];
}
- (void)hideHUD
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    self.hud = nil;
}
- (void)showMessageHUD:(NSString *)str
{
    if (!_hud) {
        self.hud = [[MBProgressHUD alloc] initWithView:self.view];
        _hud.removeFromSuperViewOnHide = YES;
    }
    _hud.mode = MBProgressHUDModeText;
    _hud.detailsLabel.text = str;
    _hud.detailsLabel.font = [UIFont boldSystemFontOfSize:15.0];
    [self.view addSubview:_hud];
    [_hud showAnimated:YES];
}
- (void)showMessageHUD:(NSString *)msg delayHide:(NSTimeInterval)delay
{
    if (!_hud) {
        self.hud = [[MBProgressHUD alloc] initWithView:self.view];
        _hud.removeFromSuperViewOnHide = YES;
    }
    [self.view addSubview:_hud];
    _hud.mode = MBProgressHUDModeText;
    _hud.detailsLabel.text = msg;
    _hud.detailsLabel.font = [UIFont boldSystemFontOfSize:15.0];
    [_hud showAnimated:YES];
    [_hud hideAnimated:YES afterDelay:delay];
}
- (void)performBlock:(void(^)())block delay:(CGFloat)delay
{
    self.delayBlock = block;
    [self performSelector:@selector(delayAction) withObject:nil afterDelay:delay];
}
- (void)delayAction
{
    if (self.delayBlock) {
        self.delayBlock();
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
